package games.ninJumpDx
{
	import core.Global;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.BlurFilter;
	import flash.geom.Point;
	
	import flashx.textLayout.elements.BreakElement;
	
	import games.ninJumpDx.data.GameData;
	import games.ninJumpDx.data.UIConfig;
	import games.ninJumpDx.tools.Tools;
	import games.ninJumpDx.ui.GameContent;
	import games.ninJumpDx.ui.factorys.BitmapButtonFactory;
	import games.ninJumpDx.ui.factorys.SimpleButtonFactory;
	import games.ninJumpDx.ui.sound.Mp3Player;
	import games.ninJumpDx.ui.sound.SoundPlayer;
	
	import interfaces.IGame;
	
	public class NinJump implements IGame
	{
		private var m_controlUi:Sprite;
		private var m_dialogUi:Sprite;
		private var m_contentUi:Sprite;
		private var m_options:Sprite;
		private var m_game:GameContent;
		
		private var m_btnCreator:SimpleButtonFactory;
		private var m_music:Mp3Player;
		private var m_sound:SoundPlayer;
		
		private var m_pause:Boolean;
		private var m_data:GameData;
				
		public function start():void
		{
			initData();
			initUi();
			initControl(true);
		}
		
		public function destroy():void
		{
			initControl(false);
			clearUi();
		
		}
		// --------------------- 数据设置---------------------------
		private function initData():void
		{
			m_pause = false;
			m_data = GameData.instance;
		}
		//------------------------界面显示--------------------------
		/*
		初始化界面
		*/
		private function initUi():void
		{
			m_contentUi = new Sprite();
			m_dialogUi  = new Sprite();
			m_controlUi = new Sprite();
			m_btnCreator = new BitmapButtonFactory();
			
			Global.stage.addChild(m_contentUi);
			Global.stage.addChild(m_dialogUi);
			Global.stage.addChild(m_controlUi);
			
			initControlUi();
			initOptions();
			initGameUi();
			initSound();
			
		}
		
		private function clearUi():void
		{
			clearContent();
			clearDialog();
			clearControl();
			
			m_dialogUi  = null;
			m_contentUi = null;
			m_controlUi = null;
		}
		
		private function clearContent():void
		{
			while(m_contentUi.numChildren > 0)
			{
				m_contentUi.removeChildAt(0);
			}
			
		}
		
		private function clearDialog():void
		{
			while(m_dialogUi.numChildren > 0)
			{
				m_dialogUi.removeChildAt(0);
			}
			
		}
		
		private function clearControl():void
		{
			while(m_controlUi.numChildren > 0)
			{
				m_controlUi.removeChildAt(0);
			}
			
		}
		
		/*
		初始化控制界面
		*/
		private function initControlUi():void
		{
			clearControl();
			var main:Bitmap = Tools.getBitmapByName("main_castle",GFrame.MAIN_UI);
			m_controlUi.addChild(main);
		
			m_controlUi.addChild(m_btnCreator.makeButton("castle_button",UIConfig.getInfoByName("castle_button").@x,UIConfig.getInfoByName("castle_button").@y));
			m_controlUi.addChild( m_btnCreator.makeButton("options_button",UIConfig.getInfoByName("options_button").@x,UIConfig.getInfoByName("options_button").@y));
		 
		}
		
		private function initGameUi():void
		{
			m_game = new GameContent(m_data);
			m_contentUi.addChild(m_game);
		}
		
		private function initSound():void
		{
			
			m_music = new Mp3Player();
			m_music.playList(["ui/sound/main.mp3"],true);
			m_sound = new SoundPlayer();
			
		}
		
		private function initOptions():void
		{
			m_options = new Sprite();
			var options_castle:Bitmap = Tools.getBitmapByName("options_castle",GFrame.MAIN_UI);
			m_options.addChild(options_castle);
			m_controlUi.addChild(m_options);
			
		    m_options.addChild(m_btnCreator.makeButton("back_button",UIConfig.getInfoByName("back_button").@x,UIConfig.getInfoByName("back_button").@y));
		
			m_options.addChild(m_btnCreator.makeButton("music_off",UIConfig.getInfoByName("music_off").@x,UIConfig.getInfoByName("music_off").@y));
			m_options.addChild(m_btnCreator.makeButton("music_on",UIConfig.getInfoByName("music_on").@x,UIConfig.getInfoByName("music_on").@y));
			m_options.addChild(m_btnCreator.makeButton("sound_off",UIConfig.getInfoByName("sound_off").@x,UIConfig.getInfoByName("sound_off").@y));
			m_options.addChild(m_btnCreator.makeButton("sound_on",UIConfig.getInfoByName("sound_on").@x,UIConfig.getInfoByName("sound_on").@y));
			m_options.visible = false;
			
			showMusicOn(GameData.instance.music);
			showSoundOn(GameData.instance.sound);
		}
		
		private function showOptions(b:Boolean):void
		{
			m_options.visible = b;
		}
		
		private function showMusicOn(b:Boolean):void
		{
			m_options.getChildByName("music_on").visible = b;
		}
		
		private function showSoundOn(b:Boolean):void
		{
			m_options.getChildByName("sound_on").visible = b;
		}
		
		private function showPauseBtn():void
		{
			m_controlUi.addChild(m_btnCreator.makeButton("pause_button",UIConfig.getInfoByName("pause_button").@x,UIConfig.getInfoByName("pause_button").@y));
		}
	
		// ------------------------------控制设置------------------------------------------------------
		
		/*
		定义控制界面的触发事件
		*/
		
		private function initControl(b:Boolean):void
		{
			if(b)
			{
				m_controlUi.addEventListener(MouseEvent.CLICK,onClickControl);
				//m_contentUi.addEventListener(MouseEvent.CLICK,onClickContent);
				//m_dialogUi.addEventListener(MouseEvent.CLICK,onClickDialog);
			}else
			{
				m_controlUi.removeEventListener(MouseEvent.CLICK,onClickControl);
				//m_contentUi.removeEventListener(MouseEvent.CLICK,onClickContent);
				//m_dialogUi.removeEventListener(MouseEvent.CLICK,onClickDialog);
			}
			m_contentUi.mouseChildren = false;
			
		}
		
		private function onClickControl(e:MouseEvent):void
		{
			trace("按钮：		",e.target.name);
			switch(e.target.name)
			{
				case "castle_button":
					clearControl();
					showPauseBtn();
					startGame();
					if(GameData.instance.music)
						m_music.playList(["ui/sound/themes/castle/intro.mp3","ui/sound/themes/castle/loop.mp3"]);
					trace("是否开放音乐：",GameData.instance.music);
					break;
				case "options_button":
					showOptions(true);
					break;
				case "back_button":
					showOptions(false);
					break;
				case "pause_button":
					m_pause = !m_pause;
					Global.pauseAllAction(m_pause);
					break ;
				case "music_on":
					showMusicOn(false);
					GameData.instance.music = false;
					m_music.setVolume(0);
					break;
				case "music_off":
					showMusicOn(true);
					GameData.instance.music = true;
					m_music.setVolume(0.1);
					break ;
				case "sound_on":
					showSoundOn(false);
					break ;
				case "sound_off":
					showSoundOn(true);
					break;
				default:
					break;
			}
		}
		
		private function onClickContent(e:MouseEvent):void
		{
		
		}
		
		private function onClickDialog(e:MouseEvent):void
		{
			
		}
		
	
		//----------------------------------------------------------------------------
		
		private function startGame():void
		{
			
			m_game.start();
		}
	}
}